// pages/cart/index.js
Page({

    /**
     * 页面的初始数据
     */
	data: {
		totalnum: "",
		editshow: false,
		cart: [{
			storeselected: "",
			storeimg: "/images/index5-1.png",
			storename: "bull精品海淘旗舰店",
			totalprice:0,
			product: [{
				proselected: false,
				img: "/images/product1.png",
				name: "小米LD5889行车记录仪单镜头高画质精品",
				price: "78.00",
				num: 2
			},
			{
				proselected: false,
				img: "/images/product1.png",
				name: "小米LD5889行车记录仪单镜头高画质精品",
				price: "78.00",
				num: 2
			},
			]
		},
		{
			storeselected: "",
			storeimg: "/images/index5-1.png",
			storename: "bull精品海淘旗舰店",
			totalprice: 0,
			product: [{
				proselected: false,
				img: "/images/product1.png",
				name: "小米LD5889行车记录仪单镜头高画质精品",
				price: "78.00",
				num: 1
			},
			{
				proselected: false,
				img: "/images/product1.png",
				name: "小米LD5889行车记录仪单镜头高画质精品",
				price: "78.00",
				num: 1
			},
			]
		}
		]
	},

    /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		this.getnum()
		this.gettotal()

	},

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
	onReady: function () {

	},

    /**
     * 生命周期函数--监听页面显示
     */
	onShow: function () {

	},
	//选择商品
	selectpro: function (e) {
		var index = e.currentTarget.dataset.index;
		var key = e.currentTarget.dataset.key;
	   var cart=this.data.cart
		cart[index].product[key].proselected = !cart[index].product[key].proselected
		console.log(cart[index].product[key].proselected)
		this.setData({
			cart:cart
		})
		this.gettotal()

	},
	//选择店铺
	selectstore: function (e) {
		var index = e.currentTarget.dataset.index;
		let cart = this.data.cart;
		cart[index].storeselected = !cart[index].storeselected
		this.setData({
			cart: cart
		})
	},
	cutnum:function(e){
		var index = e.currentTarget.dataset.index;
		var key = e.currentTarget.dataset.key;
		var cart = this.data.cart
		var num = cart[index].product[key].num
		console.log(num)
		if (num <= 1) {
			return false;
		}
		num--;
		cart[index].product[key].num=num
		this.setData({
			cart:cart
		})
		this.gettotal()
	},
	addnum:function(e){
		var index = e.currentTarget.dataset.index;
		var key = e.currentTarget.dataset.key;
		var cart = this.data.cart
		var num = cart[index].product[key].num
		num++;
		cart[index].product[key].num = num
		this.setData({
			cart: cart
		})
		this.gettotal()
	},
	editnum: function () {
		var editshow = this.data.editshow
		editshow = !editshow
		this.setData({
			editshow: editshow
		})
	},

	getnum() {
		var cart = this.data.cart
		var num = 0
		for (let i = 0; i < cart.length; i++) {
			for (let j = 0; j < cart[i].product.length; j++) {
				num += cart[i].product[j].num
			}
		}
		this.setData({
			totalnum: num
		})
	},
	gettotal() {
		var temp = this.data.cart
		for (var i = 0; i < temp.length; i++) {
			for (var j = 0; j < temp[i].product.length; j++) {
				if (temp[i].product[j].proselected==true){
					temp[i].totalprice += temp[i].product[j].num * temp[i].product[j].price
				}
				
			}
		}

		this.setData({
			cart: temp
		})
	}
})